import { login, getUserInfo, region } from '@/api/user'
import {
  getToken,
  setToken,
  removeToken,
  setTimeStamp,
  getLocalUsername,
  setLocalUsername,
  removeLocalUsername
} from '@/utils/auth'
import { resetRouter } from '@/router'

const state = {
  token: getToken(),
  userinfo: {}
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
    setTimeStamp(token)
  },
  REMOVE_TOKEN: (state) => {
    state.token = null
    removeToken()
  },
  removeUserInfo: (state) => {
    state.userinfo = {}
  },
  setUserInfo: (state, userObj) => {
    state.userinfo = { ...userObj }
  },
  updateUserInfo: (state, data) => {
    state.userinfo = { ...data }
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    // const userObj = { user_name: username, user_password: password }
    return new Promise((resolve, reject) => {
      login({ user_name: username.trim(), user_password: password }).then(response => {
        setLocalUsername(username)
        // commit('setUserInfo', userObj)
        const { token } = response
        commit('SET_TOKEN', token)
        setToken(token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  // user region
  region({ commit }, userInfo) {
    const { username, password, nickname } = userInfo
    const userObj = { user_name: username, user_password: password, nickname: nickname }
    return new Promise((resolve, reject) => {
      region({ user_name: username, user_password: password, nickname: nickname }).then(res => {
        commit('updateUserInfo', userObj)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  // get user info
  async getInfo({ commit }) {
    // return new Promise((resolve, reject) => {
    //   const userinfo = getLocalUsername() ? getLocalUsername() : { user_name: state.userInfo.user_name }
    //   (userinfo).then(response => {
    //     const { data } = response
    //     if (!data) {
    //       return reject('Verification failed, please Login again.')
    //     }
    //     commit('updateUserInfo', data)
    //     resolve(data)
    //   }).catch(error => {
    //     reject(error)
    //   })
    // })
    const userinfo = getLocalUsername() ? getLocalUsername() : { user_name: state.userInfo.user_name }
    try {
      const res = await getUserInfo(userinfo)
      commit('updateUserInfo', res.data)
      return res.data
    } catch (error) {
      console.log('error', error)
    }
  },
  // async getInfo({commit}) {

  // },

  // user logout
  logout({ commit }) {
    commit('REMOVE_TOKEN')
    commit('removeUserInfo')
    removeLocalUsername()
    resetRouter()
    commit('permission/setRouters', [], { root: true })
  }

  // // remove token
  // resetToken({ commit }) {
  //   return new Promise(resolve => {
  //     removeToken() // must remove  token  first
  //     commit('RESET_STATE')
  //     resolve()
  //   })
  // }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

